﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using BrokerBase.Lib.Tool;

namespace Broker.View.ViewModel
{
    public class PersonNameModel
    {
        public static IList<PersonNameModel> LoadAllPerson()
        {
            
            using (ISession sess= LibGlobal.HbmSessionFactory.OpenSession())
            {
                string sql = null;

                if (LibGlobal.AppType == AppType.Business)
                    sql = "select id,name,birthday,sex from Person order by name collate PinYin";
                else
                    sql = "select id,name,birthday,sex from Person order by name";

                IList<object[]> ls = sess.CreateSQLQuery(sql).List<object[]>();

                int m = ls.Count;

                IList<PersonNameModel> result = new List<PersonNameModel>(m==0?1:m);

                foreach (object[] obj in ls)
                {
                    PersonNameModel pnm = new PersonNameModel();
                    pnm.Id = Convert.ToInt32(obj[0]);
                    pnm.Name = Convert.ToString(obj[1]);
                    pnm.Birthday = Convert.ToDateTime(obj[2]);
                    pnm.SexId = Convert.ToInt32(obj[3]);
                    result.Add(pnm);
                }

                return result;
            }
        }

        public int Id { get; set; }

        public string Name { get; set; }

        public DateTime Birthday { get; set; }

        public int SexId { get; set; }

        public int Age
        {
            get 
            {
                return PersonUtil.GetAgeFromBirthdayAndTime(this.Birthday, DateTime.Now);
                
            }
        }

        public string AgeStr
        {
            get { return Age + "岁"; }
        }

        public string SexName
        {
            get
            {
                return SexId == 1 ? "男" : "女";
            }
        }
    }
}
